﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace CMS.Core {
	public class Html {
		/// <summary>
		/// 对一些不更新的数据进行缓存
		/// </summary>
		private static Dictionary<string, string> _cache = new Dictionary<string, string>();

		#region Nav

		/// <summary>
		/// 生成 Nav 导航
		/// </summary>
		/// <returns></returns>
		public static string GenNav() {
			if (_cache.ContainsKey("GenNav"))
				return _cache["GenNav"];

			StringBuilder html = new StringBuilder("<ul>");
			
			// 查找主菜单
			var main_nav = Core.Category.GetMainItems();

			foreach (var main in main_nav) {
				html.AppendFormat("<li><a href=\"javascript:;\">{0}</a>", main.Title);

				// 根据主菜单ID查找子菜单
				var sub_nav = Core.Category.GetSubItems(main.ID);

				if (sub_nav.Count > 0) {
					html.Append("<ul>");

					foreach (var sub in sub_nav) {
						html.AppendFormat("<li><a href=\"{1}\">{0}</a></li>", sub.Title, sub.Url);
					}

					html.Append("</ul>");
				}

				html.Append("</li>");
			}

			html.Append("</ul>");

			_cache["GenNav"] = html.ToString();
			return _cache["GenNav"];
		}

		/// <summary>
		/// 生成不带显示文字的导航, 需要显示的文字, 可由背景以图片方式显示出来
		/// </summary>
		/// <returns></returns>
		public static string GenNavWithoutText() {
			if (_cache.ContainsKey("GenNavWithoutText"))
				return _cache["GenNavWithoutText"];

			StringBuilder html = new StringBuilder("<ul>");

			// 查找主菜单
			var main_nav = Core.Category.GetMainItems();

			foreach (var main in main_nav) {
				html.AppendFormat("<li><a href=\"javascript:;\">{0}</a>", main.Title);

				// 根据主菜单ID查找子菜单
				var sub_nav = Core.Category.GetSubItems(main.ID);

				if (sub_nav.Count > 0) {
					html.Append("<ul>");

					foreach (var sub in sub_nav) {
						html.AppendFormat("<li><a href=\"{1}\">{0}</a></li>", sub.Title, sub.Url);
					}

					html.Append("</ul>");
				}

				html.Append("</li>");
			}

			html.Append("</ul>");

			_cache["GenNavWithoutText"] = html.ToString();
			return _cache["GenNavWithoutText"];
		}

		#endregion

		/// <summary>
		/// 解析分页的 Article 数据
		/// </summary>
		/// <param name="categoryId"></param>
		/// <param name="pageIndex"></param>
		/// <param name="pageSize"></param>
		/// <returns></returns>
		public static string ParseArticles(int categoryId, int pageIndex, int pageSize) {
			var articles = Core.Article.GetPagerList(categoryId, pageIndex, pageSize);

			StringBuilder html = new StringBuilder();
			string format = "<li><a href='article.aspx?cid={0}&id={1}' target='_blank' title='{2}'>{2}</a><span>{3}</span></li>";

			html.Append("<ul>");

			foreach (var art in articles) {
				html.AppendFormat(format, categoryId, art.ID, art.Title, art.AddTime.ToString("yyyy-MM-dd"));
			}

			html.Append("</ul>");

			if (html.Length < 10)
				return "<div style='width:600px;height:300px;background:url(../images/nodata.gif) 190px 0 no-repeat;'></div>";

			return html.ToString();
		}

		/// <summary>
		/// 解析分页的 Article 数据
		/// </summary>
		/// <param name="categoryId"></param>
		/// <param name="pageIndex"></param>
		/// <param name="pageSize"></param>
		/// <returns></returns>
		public static string ParseArticles2(int categoryId, int pageIndex, int pageSize) {
			var articles = Core.Article.GetPagerList(categoryId, pageIndex, pageSize);

			StringBuilder html = new StringBuilder();
			string format = "<li><a href='article.aspx?cid={0}&id={1}' target='_blank' title='{2}'>{2}</a><span>{3}</span></li>";

			html.Append("<ul>");

			foreach (var art in articles) {
				html.AppendFormat(format, categoryId, art.ID, art.Title, art.AddTime.ToString("yyyy-MM-dd"));
			}

			html.Append("</ul>");

			if (html.Length < 10)
				return "<div style='width:600px;height:300px;background:url(../images/nodata.gif) 190px 0 no-repeat;'></div>";

			return html.ToString();
		}

		/// <summary>
		/// 根据打开的栏目, 解析生成 SiteMapPath
		/// </summary>
		/// <param name="categoryId"></param>
		/// <returns></returns>
		public static string ParseSitePath(int categoryId) {
			Model.Category category2 = Core.Category.GetModel(categoryId);
			Model.Category category1 = Core.Category.GetModel(category2.ParentID);

			string html = "<a href='javascript:;'>{1}</a>&nbsp;&nbsp;>&nbsp;&nbsp;<a href='{2}'>{3}</a>";
			return string.Format(html, category1.Url, category1.Title, category2.Url, category2.Title);
		}

		/// <summary>
		/// 根据打开的栏目, 解析生成各页面的 Title
		/// </summary>
		/// <param name="categoryId"></param>
		/// <returns></returns>
		public static string ParsePageTitle(int categoryId) {
			Model.Category category2 = Core.Category.GetModel(categoryId);
			Model.Category category1 = Core.Category.GetModel(category2.ParentID);

			return category1.Title + " - " + category2.Title;
		}

		/// <summary>
		/// 根据打开的栏目, 解析生成侧边栏的菜单列表
		/// </summary>
		/// <param name="categoryId"></param>
		/// <returns></returns>
		public static string ParseSidebar(int categoryId) {
			Model.Category category2 = Core.Category.GetModel(categoryId);
			var subItems = Core.Category.GetSubItems(category2.ParentID);
			string format = "<li><a href='{0}'>{1}</a></li>";
			StringBuilder html = new StringBuilder();

			html.Append("<ul>");

			foreach (var item in subItems) {
				html.AppendFormat(format, item.Url, item.Title);
			}

			html.Append("</ul>");

			return html.ToString();
		}
	}
}
